package com.google.appengine.codelab;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

@SuppressWarnings("serial")
public class UserStory08Servlet extends BaseServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp); 
	}
	
	private static List<String> fieldAsList(String kind, String propertyName, boolean distinct) {
		List<String> result = new ArrayList<String>();
		Query query = new Query(kind);
		for (Entity e : Util.getDatastoreServiceInstance().prepare(query).asIterable()) {
			if (distinct) {
				if (!result.contains(e.getProperty(propertyName).toString().toUpperCase())) {
					result.add(e.getProperty(propertyName).toString().toUpperCase());
				}
			} else {
				result.add(e.getProperty(propertyName).toString());
			}
		}
		return result;
	}
	
	public static List<String> getFraudeFormaDeteccao(boolean distinct) {
		return fieldAsList("Fraude", "fraude_forma_deteccao", distinct);
	}
	
	public static List<String> getFraudeType(boolean distinct) {
		return fieldAsList("Fraude", "fraude_type", distinct);
	}
	
	public static List<String> getTransType(boolean distinct) {
		return fieldAsList("Transacao", "trans_type", distinct);
	}
	
	public static List<Entity> getFraudes(String fraude_type, String fraude_forma_deteccao,
			String data_ini, String data_fin, String trans_type) {
		DatastoreService ds = Util.getDatastoreServiceInstance();
		List<Entity> result = new ArrayList<Entity>();
		List<Filter> filter = new ArrayList<Filter>();
		Query query = new Query("Fraude");
		if (fraude_type != "") {
			filter.add(new FilterPredicate("fraude_type", FilterOperator.EQUAL, fraude_type));
		}
		if (fraude_forma_deteccao != "") {
			filter.add(new FilterPredicate("fraude_forma_deteccao", FilterOperator.EQUAL,
					fraude_forma_deteccao));
		}
		if (data_ini != "") {
			filter.add(new FilterPredicate("fraude_data_deteccao",
					FilterOperator.GREATER_THAN_OR_EQUAL, data_ini));
		}
		if (data_fin != "") {
			filter.add(new FilterPredicate("fraude_data_deteccao",
					FilterOperator.LESS_THAN_OR_EQUAL, data_fin));
		}
		if (filter.size() > 0) {
			query.setFilter(filter.size() == 1 ? filter.get(0) : CompositeFilterOperator.and(
					filter));
		}
		for (Entity fraude : ds.prepare(query).asIterable()) {
			if (trans_type != "") {
				filter.clear();
				filter.add(new FilterPredicate("trans_id", FilterOperator.EQUAL,
						fraude.getProperty("trans_id").toString()));
				filter.add(new FilterPredicate("trans_type", FilterOperator.EQUAL,
						trans_type));
				query = new Query("Transacao");
				query.setFilter(CompositeFilterOperator.and(filter));
				Entity trans = ds.prepare(query).asSingleEntity();
				if (trans == null) {
					continue;
				}
			}
			result.add(fraude);
		}
		return result;
	}
	
}